//
//  NetworkCommand.m
//  walletSDK
//
//  Created by xuyf on 13-6-17.
//  Copyright (c) 2013年 LianLian. All rights reserved.
//

#import "LLNetworkCommand.h"


#import "NSString+Additions.h"
//#import "LLSystemConfig.h"
//#import "LLAlertService.h"
//#import "LLTradeSession.h"
//#import "UIDevice+IdentifierAddition.h"
//#import "LLYTGlobalDefine.h"
//#import "SSKeychain.h"
//#import "LLYTPhoneUserSession.h"
//#import "LLYTUserLoginService.h"
//#import "LLYTSystemManager.h"
//#import "LLYTGlobalDefine.h"

typedef enum RequestType{
    kTransCodeLast,
    kSimpleRequestTag = kTransCodeLast + 1,
    
}RequestType;


@implementation LLNetworkCommand

- (void)dealloc
{
    self.functionName = nil;
    self.transCode = nil;
    self.paramDic = nil;
    self.businessSuccessHandler = nil;
    self.businessErrorHandler = nil;
    self.errorHandler = nil;
    self.systemHoldTip = nil;
    self.systemErrorAlertTip = nil;
}

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        self.paramDic = [NSMutableDictionary dictionary];
        
        // TODO:
        self.isGet = NO;
        self.needTraderNO = NO;
        self.systemHoldTip = @"正在加载中";
        
        self.useSystemHold = YES;
        self.useSystemErrorAlert = YES;
        self.systemErrorAlertTip = @"网络链接超时，请检查您的网络并重新提交";
    }
    return self;
}
/*
 
 #define BASE_PHONE_URL @"https://yintong.com.cn/phone360"
 #define kTraderNO   @"201310241000001004" //201310241000001004
 #define kPhoneSignKey   @"58_pay_mobilesafe_1016"
 //#define BASE_URL @"https://yintong.com.cn/phone"
 #define BASE_URL @"https://yintong.com.cn/secure_server"
 #define MORE_URL @"https://yintong.com.cn/STATIC"
 #define kSignKey    @"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAML"

 */
- (NSString*)valueWithKey:(NSString*)paramKey
{
    if ([paramKey isEqualToString:[self signKeyTitle]]) {
        return [[self signKey] URLEncodedString];
    }
    
    return self.paramDic[paramKey];
}

- (void)prepareParamDic
{
//    if ([LLTradeSession currentSession].userInfo.token != nil)
//    {
//        self.paramDic[@"token"] = [LLTradeSession currentSession].userInfo.token;
////        self.paramDic[@"oid_userno"] = [LLTradeSession currentSession].userInfo.oid_userno;
//    }
    
//    if (self.needTraderNO && self.paramDic[@"traderno"] == nil)
//    {
//        self.paramDic[@"traderno"] = kTraderNO;
//    }
//    
//    self.paramDic[@"sign_type"] = @"MD5";
//    
//    self.paramDic[@"imei_request"] = [LLYTSystemManager deviceBindID];
//
//    self.paramDic[@"machine_id"] = [LLYTSystemManager machineID];
//    
//    self.paramDic[@"pay_chnl"] = @"10";     // 10：手机应用渠道(sdk默认), 13：个人WEB, 15：API, 16：WAP
//    NSString *CHNNAL_IOS = @"2";
//    self.paramDic[@"flag_chn"] = CHNNAL_IOS;
//    self.paramDic[@"ver_app"] = [self ver_app];
}


- (NSString*)paramStringWithKeyArray:(NSArray*)keyArray
{
    NSMutableString *paramString = [NSMutableString stringWithString:@""];
    
    for (NSString *key in keyArray)
    {
        if ([[self valueWithKey:key] length]!=0){
            [paramString appendFormat:@"&%@=%@", key, [self valueWithKey:key]];
        }
    }
    
    if ([paramString length] > 1)
    {
        [paramString deleteCharactersInRange:NSMakeRange(0, 1)];    // remove '&'
    }
    return paramString;
    
}

- (NSString*)paramString
{
    return [self paramStringWithKeyArray:[self.paramDic allKeys]];
}

- (void)urlEncodeParamDic
{
    for (NSString *key in [self.paramDic allKeys])
    {
        self.paramDic[key] = [self.paramDic[key] URLEncodedString];
    }
}

- (NSString*)signature
{
    NSMutableArray *keyArray = [NSMutableArray arrayWithArray:[self.paramDic allKeys]];
    [keyArray sortUsingSelector:@selector(localizedCompare:)];
    
    // 按照算法sign_key 不参与排序
    [keyArray addObject:[self signKeyTitle]];
    
    NSString *sortedParamString = [self paramStringWithKeyArray:keyArray];
    
    NSString *signature = [sortedParamString MD5String];
    
    return signature;
}

- (NSString*)signKey
{
    // SDK key
    if (self.testMode) {
        return @"201103171000000000";
    }
    return @"34373233343338373739303333313235383536";
}

- (NSString*)signKeyTitle
{
    return @"sign_key";
}

- (NSString*)signatureTitle
{
    return @"sign";
}

- (NSString*)ver_app
{
    return kLLWalletSDKVersion;
}

- (BOOL)isGetMethod
{
    return self.isGet;
}

- (BOOL)isTestSecureMode
{
    return NO;
}

- (NSDictionary*)functionInfoDic
{
    return @{
             // functionName:[transcode, url, retIsList, needLogin]
             @"TRANS_ACTIVATE_ACCOUNT":@[@"3002", @"/llwallet/outside/activateAccount.htm"],
             //@"TRANS_TELEPHONEFARELIST":@[@"2008",@"/mall/productquery.htm"],
             };
}

- (void)prepareForSend
{
    NSString *functionName = self.functionName;
    
    NSArray *transInfoDic = [self functionInfoDic][functionName];
    
    if (self.transCode == nil)
    {
        self.transCode = transInfoDic[0];
    }
    
    self.pathUrl = transInfoDic[1];
    
    [self.paramDic setValue:self.transCode forKey:@"transcode"];
    
    [self prepareParamDic];
    
    [self urlEncodeParamDic];
    
    [self.paramDic setValue:[self signature] forKey:[self signatureTitle]];
}

- (BOOL)isUseCmdResultParser
{
    return NO;
}

- (void)handleResultObj:(id)jsonObj
{
    
}

+ (instancetype)cmd
{
    return [[self alloc] init];
}

@end
